/*
 * Copyright (C) 2009 AnK Team
 *	 Andrey Korolev
 *	 Anna Krel
 *	 Anna Kapanina
 *	 
 *	 http://code.google.com/p/ankfood/	
 *    
     
 *    Licensed under the GNU General Public License, Version 2.0 (the "License");
 *
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *         http://www.gnu.org/licenses/gpl-2.0.html

 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,

 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */



package org.ank.food;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

public class ProductDataManager {
	private volatile static ProductDataManager instance;
	
	public static final int STANDARD_WEIGHT = 100;
	private static final int MAX_WEIGHT_ITEMS_PER_PRODUCT = 45;
	private static final String PREFS_NAME = "prodman";
	private static final String PREFS_PROT_QUOTA = "prodman prot qouta";
	private static final String PREFS_FAT_QUOTA = "prodman fat qouta";
	private static final String PREFS_CARBO_QUOTA = "prodman carbo qouta";
	
//	private static int[] NOT_DESIRABLE_GROUPS = {19,21,22,25,35};
//	private static String[] NOT_DESIRABLE_NAMES = {"alcohol"};
	private static final int[] NOT_RECOMMENDED_GROUPS_IDS = {18,19,21,25};
	private int protQuota, fatQuota, carboQuota;
	

	private Context context;
	private ProductDataManager(Context c) {
		this.context = c;
	}
 
	public static ProductDataManager getInstance(Context context) {
        if (instance == null) {
            synchronized (ProductDataManager.class) {
                if (instance == null) {
                    instance = new ProductDataManager(context);
                }
            }
        }
        return instance;
    }
	
	public void setProtQuota(int a){
		protQuota = a;
		
		SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
    	editor.putInt(PREFS_PROT_QUOTA, protQuota);
    	editor.commit();
		
	}
	public void setFatQuota(int a){
		fatQuota = a;
		
		SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
    	editor.putInt(PREFS_FAT_QUOTA, fatQuota);
    	editor.commit();
	}
	public void setCarboQuota(int a){
		carboQuota = a;
		
		SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
    	editor.putInt(PREFS_CARBO_QUOTA, carboQuota);
    	editor.commit();
	}
	
	
//	this method returns the name of product from productGroup group, whose parametrs
//	do not exceed the qoutas set by set[qoutaName](); 
	public String[] getSuitableProductNameAndId(int group){
		int length, pos=0;
		String name,id;
//		//Log.v("findSuitableProduct","quotas = "+protQuota+" "+fatQuota+" "+carboQuota);
		Cursor cursor = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID],
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_ENERGY],
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_ENERGY],
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_ENERGY]
				},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.GROUP_ID]+
				" = "+String.valueOf(group)+" AND "+
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_ENERGY]+
				" < "+String.valueOf(protQuota)+" AND "+
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_ENERGY]+
				" < "+String.valueOf(fatQuota)+" AND "+
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_ENERGY]+
				" < "+String.valueOf(carboQuota),
				null, null);
		if ( ( length = cursor.getCount() ) == 0){
			//Log.v("findSuitableProduct","count = 0");
			return null;
		}
//		//Log.v("findSuitableProduct","length = "+length);
//		pos = (int)Math.round(((length-1) * Math.random()));
//		//Log.v("findSuitableProduct","pos = "+pos);
		
		
		pos = (int)Math.round(((length-1) * Math.random()));
		
		cursor.moveToPosition(pos);
//		protQuota -= cursor.getInt(1); 
		setProtQuota(protQuota-cursor.getInt(2));
		cursor.moveToPosition(pos);
//		fatQuota -= cursor.getInt(2);
		setFatQuota(fatQuota-cursor.getInt(3));
		cursor.moveToPosition(pos);
//		carboQuota -= cursor.getInt(3);
		setCarboQuota(carboQuota-cursor.getInt(4));
		cursor.moveToPosition(pos);
		name = cursor.getString(1);
		id = String.valueOf(cursor.getInt(0));
		cursor.close();
//		//Log.v("findSuitableProduct","name = "+name);
		return new String[]{id,name}; 	
	}
	
	
	
	
//	public String getSuitableProductName(int group){
//		int length, pos=0;
//		String name;
////		//Log.v("findSuitableProduct","quotas = "+protQuota+" "+fatQuota+" "+carboQuota);
//		Cursor cursor = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
//				new String[]{
//					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
//					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_ENERGY],
//					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_ENERGY],
//					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_ENERGY]
//				},
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.GROUP_ID]+" = "+String.valueOf(group)+
//				" and "+
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_ENERGY]+" < "+String.valueOf(protQuota)+
//				" and "+
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_ENERGY]+" < "+String.valueOf(fatQuota)+
//				" and "+
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_ENERGY]+" < "+String.valueOf(carboQuota),
//				new String[]{}, null);
//		if ( ( length = cursor.getCount() ) == 0){
//			//Log.v("findSuitableProduct","count = 0");
//			return null;
//		}
////		//Log.v("findSuitableProduct","length = "+length);
////		pos = (int)Math.round(((length-1) * Math.random()));
////		//Log.v("findSuitableProduct","pos = "+pos);
//		
//		
//		pos = (int)Math.round(((length-1) * Math.random()));
//		
//		cursor.moveToPosition(pos);
//		protQuota -= cursor.getInt(1);
//		cursor.moveToPosition(pos);
//		fatQuota -= cursor.getInt(2);
//		cursor.moveToPosition(pos);
//		carboQuota -= cursor.getInt(3);
//		cursor.moveToPosition(pos);
//		name = cursor.getString(0);
////		//Log.v("findSuitableProduct","name = "+name);
//		return name; 	
//	}

	
	
	
	public int getProductIdByName(String name){
		//Log.v("findIdByName","finfing id for name: "+name);
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]}, 
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+" = \""+name+"\"",
				null, null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public String getProductNameById(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]}, 
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]+" = "+
					String.valueOf(id),
				null, null);
		c.moveToFirst();
		String s = c.getString(0);
		c.close();
		return s;
	}
	
	public int getProductProtEnergy(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
				new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_ENERGY]},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID] + " = "+String.valueOf(id), 
				null, null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	public int getProductFatEnergy(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
				new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_ENERGY]},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID] + " = "+String.valueOf(id), 
				null, null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	public int getProductCarboEnergy(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
				new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_ENERGY]},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID] + " = "+String.valueOf(id), 
				null, null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public Cursor getAllProductNames(){
		return context.getContentResolver().query( ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
				}, null, null, null);
	}
	
	public Cursor getGroupsNames(){
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_GROUPS,
				new String[]{ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.ID],
				ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.GROUP_NAME]}, 
				null,null,
				ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.GROUP_NAME]);
	}
	
	public Cursor getSubgroupsNames(int group_id){
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_SUBGROUPS,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.SUBGROUP_NAME_SUBGROUPS],
					ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.ID]
				}, 
				ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.GROUP_ID_SUBGROUPS]+
				" = " + String.valueOf(group_id) ,null,
				ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.SUBGROUP_NAME_SUBGROUPS]);
	}
	
	
	public String getGroupNameById(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_GROUPS,
				new String[]{ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.GROUP_NAME]},
				ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.ID] + " = " + id, 
				null,null);
		c.moveToFirst();
		String s = c.getString(0);
		c.close();
		return s;
	}
	
	public String getSubgroupNameById(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_SUBGROUPS,
				new String[]{ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.
				                                                        SUBGROUP_NAME_SUBGROUPS]},
				ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.ID] + " = " + id, 
				null,null);
		c.moveToFirst();
		String s = c.getString(0);
		c.close();
		return s;
	}
	
	public Cursor getProductsBySubgroup(int subgroupId){
		//find the name of sub group;
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_SUBGROUPS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.SUBGROUP_NAME_SUBGROUPS]
			}, 
			ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.ID]+
			" = " + String.valueOf(subgroupId) ,null,null);
		c.moveToFirst();
		String name = c.getString(0);
		c.close();
		
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
			}, 
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.SUBGROUP]+
			" = \"" + name + "\"" ,null,
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]);
	}
	
	
	
	public Cursor getProductsByGroup(int groupId){
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
			}, 
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.GROUP_ID]+
			" = " + groupId ,null,
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+" ASC");
	}
	
	public Cursor getProductsSearchResult(String constraint){
				
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
			}, 
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+
			" LIKE \"%"+constraint+"%\"", null,
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]);
	}
	
	public Cursor getProductsBySubgroupFiltered(int subgroupId, String constraint){
		//find the name of sub group;
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_SUBGROUPS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.SUBGROUP_NAME_SUBGROUPS]
			}, 
			ProductDataProvider.COLUMN_NAMES_SUBGROUPS[ProductDataProvider.ID]+
			" = " + String.valueOf(subgroupId) ,null,null);
		c.moveToFirst();
		String name = c.getString(0);
		c.close();
		
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
			}, 
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.SUBGROUP]+
			" = \"" + name + "\" AND "+
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+
			" LIKE \"%"+constraint+"%\"",null,
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]);
	}
	
	public Cursor getProductsByGroupFiltered(int groupId, String constraint){
		//find the name of sub group;
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
			new String[]{
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
			}, 
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.GROUP_ID]+
			" = " + groupId + " AND "+
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+
			" LIKE \"%"+constraint+"%\"",null,
			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]);
	}
	
//	public Cursor searchAndShowGroups(String constraint){
//		//find the name of sub group;
//		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
//			new String[]{
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME],
//				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]
//			}, 
//			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.GROUP_ID]+
//			" = " + groupId + " AND "+
//			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+
//			" LIKE \"%"+constraint+"%\"",null,
//			ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]);
//	}
	
	public Cursor getWeightItemsForProduct(int prodId){
		return context.getContentResolver().query(ProductDataProvider.CONTENT_URI_WEIGHT,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.ID],
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_AMOUNT],
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_NAME],
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]
				},
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_PROD_ID]+
				" = "+prodId, null, 
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]+" ASC");
	}
	
	public float getWeightItemWeightById(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_WEIGHT,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]
				},
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.ID]+
				" = "+id, null, null);
		c.moveToFirst();
		float f = c.getFloat(0);
		c.close();
		return f;
	}
	 
	public String getWeightItemNameById(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_WEIGHT,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_NAME],
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_AMOUNT]
				},
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.ID]+
				" = "+id, null, null);
		c.moveToFirst();
		StringBuffer buf = new StringBuffer(c.getString(1));
		buf.append(" ").append(c.getString(0));
		c.close();
		return buf.toString();
	}
	
	
	
	public float getMaxItemWeightForProduct(int prodId){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_WEIGHT,
				new String[]{
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]
				},
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_PROD_ID]+
				" = "+prodId, null,
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]+
				" DESC");
		c.moveToFirst();
		float f = c.getFloat(0);
		c.close();
		return f;
	}
	
	public float countWeight(int weightItemId, int amount){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_WEIGHT, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.WEIGHT_WEIGHT]
				},
				ProductDataProvider.COLUMN_NAMES_WEIGHT[ProductDataProvider.ID]+
				" = " + weightItemId, null,null);
		c.moveToFirst();
		float f = c.getFloat(0);
		c.close();
		return f*amount;
		
	}
	public boolean isAcceptable(int prodId, int weightItemId, int amount){
		float weight = countWeight(weightItemId, amount);
		ProfileManager pm = ProfileManager.getInstance(context);
		MenuManager am = MenuManager.getInstance(context);
		int newEatenProtEnergy = am.getMenuProtEnergy() + 
				Math.round(weight / STANDARD_WEIGHT * getProductProtEnergy(prodId));
		int newEatenFatEnergy = am.getMenuFatEnergy() + 
				Math.round(weight / STANDARD_WEIGHT * getProductFatEnergy(prodId));
		int newEatenCarboEnergy = am.getMenuCarboEnergy() + 
				Math.round(weight / STANDARD_WEIGHT * getProductCarboEnergy(prodId));
		float coeff = (float) (MenuActivity.MAX_PERCENTAGE/100.0);
		return (
			newEatenProtEnergy < (pm.getProtQuota()*coeff) &&
			newEatenFatEnergy < (pm.getFatQuota()*coeff) &&
			newEatenCarboEnergy < (pm.getCarboQuota()*coeff) 
		);
	}
	
	public int getProductProtValue(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.PROT_CONTENT]
				},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]+
				" = " + id, null , null);
//		//Log.v("prodMan","looking for protContent in id = "+id);
//		//Log.v("prodMan","cursor length = "+c.getCount());
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public int getProductFatValue(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.FAT_CONTENT]
				},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]+
				" = " + id, null , null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public int getProductCarboValue(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.CARBO_CONTENT]
				},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]+
				" = " + id, null , null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public int getProductEnergyValue(int id){
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ENERGY]
				},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]+
				" = " + id, null , null);
		c.moveToFirst();
		int a = c.getInt(0);
		c.close();
		return a;
	}
	
	public StringBuffer getRecommendedWeightAsStrBuf(int prodId){
		Cursor weightItemsCursor = getWeightItemsForProduct(prodId);
		int amount = 0;
		weightItemsCursor.moveToFirst();
		int weightItemId = weightItemsCursor.getInt(0);
		
		
//		weightItemsCursor.moveToFirst();
//		while(amount < MAX_WEIGHT_ITEMS_PER_PRODUCT){
//			if(!isAcceptable(prodId, weightItemsCursor.getInt(0), (amount+1)))
//				break;
//			amount++;
//		}
		amount = getRecommendedAmountForWeightItem(prodId, weightItemId); 
		if (amount > 0){
			String s = weightItemsCursor.getString(2);
			weightItemsCursor.close();
			return new StringBuffer(amount+" "+s);
		}
		weightItemsCursor.close();
		return null;
	}
	
	public int[] getRecommendedWeightAsIntArr(int prodId){
		//Log.v("getRecasInt[]","prodId = "+prodId);
		Cursor weightItemsCursor = getWeightItemsForProduct(prodId);
		if(weightItemsCursor.getCount() == 0){
			//Log.v("getRec","FAIL");
			weightItemsCursor.close();
			return null;
		}
//		//Log.v("getRec","weighItemsC.count = "+weightItemsCursor.getCount());
		int amount = 0;
		weightItemsCursor.moveToFirst();
		int weightItemId = weightItemsCursor.getInt(0);

		amount = getRecommendedAmountForWeightItem(prodId, weightItemId); 
		if (amount > 0){
			int[] result = new int[2];
			result[0] = weightItemsCursor.getInt(0);
			result[1] = amount;
			weightItemsCursor.close();
			return result;
		}
		weightItemsCursor.close();
		return null;
	}
	
	public int getRecommendedAmountForWeightItem(int prodId, int weightItemId){
		int amount = 0;
		while(amount < MAX_WEIGHT_ITEMS_PER_PRODUCT){
			if( !isAcceptable(prodId, weightItemId, (amount+1) ) )
				break;
			amount++;
		}
		return amount;
	}
	
	public boolean databaseExists(){
		try{
            FileInputStream file = new FileInputStream(ProductDataProvider.DATABASE_LOCATION+
                            ProductDataProvider.DATABASE_NAME);
            file.close();
		}catch(FileNotFoundException e){
			return false;
		} catch (IOException e) {
			return false;    
		}
		return true;
	}

	public Cursor getRecommendedGroupsCursor(){
		//make WHERE string
		StringBuffer where = new StringBuffer();
		where.append(ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.ID]).
				append(" NOT IN (");
		for (int i=0; i < NOT_RECOMMENDED_GROUPS_IDS.length - 1 ; i++ ){
			where.append(NOT_RECOMMENDED_GROUPS_IDS[i]).append(',');
		}
		where.append(NOT_RECOMMENDED_GROUPS_IDS[NOT_RECOMMENDED_GROUPS_IDS.length-1]).
				append(')');
		Log.v("getRecGroups", "where str = "+where);
		
		Cursor c = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_GROUPS, 
				new String[]{
					ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.ID],
					ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.GROUP_NAME]
				},
				where.toString(), null , 
				ProductDataProvider.COLUMN_NAMES_GROUPS[ProductDataProvider.GROUP_NAME]+" asc");
		return c;
	}
	

}
